package com.zz.ecommerce.declareManage.generateDeclare;

import com.zz.ecommerce.common.bean.qdc.db.TGoodsListQdc;
import com.zz.ecommerce.common.bean.qdc.db.TOrderHeadQdc;
import com.zz.ecommerce.common.bean.qdc.db.TWaybillHeadQdc;
import com.zz.ecommerce.common.bean.qdc.db.TWaybillListQdc;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.QdcGoodsListDao;
import com.zz.ecommerce.declareManage.common.dao.QdcOrderHeadDao;
import com.zz.ecommerce.declareManage.common.dao.QdcWaybillListDao;
import com.zz.ecommerce.declareManage.common.dao.TWaybillHeadQdcDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

/**
 * Created by admin on 2016/3/30.
 * <p>
 * 生成申报单Controller
 */
@Controller
@RequestMapping("/generateDeclare")
@Scope("prototype")
public class GenerateDeclareController {

    @Autowired
    private QdcOrderHeadDao orderHeadDao;

    @Autowired
    private QdcGoodsListDao goodsListDao;

    @Autowired
    private QdcWaybillListDao waybillListDao;

    @Autowired
    private GenerateDeclareService generateDeclareService;

    @Autowired
    private TWaybillHeadQdcDao tWaybillHeadQdcDao;

    @RequestMapping(value = "generateDeclareByOrderId")
    @ResponseBody
    public String generateDeclareByOrderId(String id, HttpSession session) {
        String msg = "";
        try {
            String[] idArr = id.split(",");
            for (int i = 0; i < idArr.length; i++) {
                TOrderHeadQdc query = new TOrderHeadQdc();
                query.setId(idArr[i]);
                //根据订单Id获得订单表头
                TOrderHeadQdc orderHead = orderHeadDao.query(TableConstants.T_ORDER_HEAD_QDC, query);

                //根据订单ID获得订单表体
                TGoodsListQdc goodsListQdc = new TGoodsListQdc();
                goodsListQdc.setRelatedId(idArr[i]);
                List<TGoodsListQdc> goodsList = goodsListDao.queryAll(TableConstants.T_GOODS_LIST_QDC, goodsListQdc, "");

                //根据订单ID获得相关联的运单
                TWaybillListQdc waybillListQdc = new TWaybillListQdc();
                waybillListQdc.setReleatedId(idArr[i]);
                List<TWaybillListQdc> waybillList = waybillListDao.queryAll(TableConstants.T_WAYBILL_LIST_QDC, waybillListQdc, "");

                if (waybillList.size() == 0) {
                    msg += "订单编号[" + orderHead.getOrderId() + "]没有运单信息;<br/>";
                    continue;
                }

                //根据运单编号+物流企业代码获得运单表头
                TWaybillHeadQdc tWaybillHeadQdc = new TWaybillHeadQdc();
                tWaybillHeadQdc.setWaybillId(waybillList.get(0).getWaybillId());
                tWaybillHeadQdc.setLogiEnteCode(waybillList.get(0).getLogiEnteCode());
                tWaybillHeadQdc.setIfCreate("0");
                TWaybillHeadQdc wayHead = tWaybillHeadQdcDao.query(TableConstants.T_WAYBILL_HEAD_QDC, tWaybillHeadQdc);

                if (wayHead == null) {
                    msg += "订单编号[" + orderHead.getOrderId() + "]没有运单信息;<br/>";
                    continue;
                }

                //生成清单入库
                generateDeclareService.generateDeclare(orderHead, wayHead, goodsList, session);

            }
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            e.printStackTrace();
            return sw.toString();
        }
        return msg;
    }
}
